package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.common.Result;
import com.yqg.education.entity.Meeting;
import com.yqg.education.entity.Period;
import com.yqg.education.mapper.PeriodMapper;
import com.yqg.education.service.IPeriodService;
import com.yqg.education.utils.QRCodeUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.List;

/**
 * PeriodController 前端控制器
 *
 * @author 张芸博
 * @since 2025-04-10
 */
@RestController
@RequestMapping("/period")
public class PeriodController {
    @Resource
    private IPeriodService periodService;

    @Resource
    private PeriodMapper periodMapper;

    @Value("${files.upload.path}")
    private String fileUploadPath;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    public Result save(@RequestBody Period period) {
        periodService.saveOrUpdate(period);
        String codeToFile = QRCodeUtil.createCodeToFile("/pages/course/list?courseId=" + period.getCourseId() + "&periodId=" + period.getId(), new File(fileUploadPath), "period" + period.getId() + ".jpg", period.getName());
        period.setCode("/api/file/download/" + codeToFile);
        return Result.success(periodService.saveOrUpdate(period));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        Period period = periodService.getById(id);
        period.setIsDelete(1);
        return Result.success(periodService.saveOrUpdate(period));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Period> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<Period> list = periodService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(periodService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String name,
                           Integer courseId) {
        pageNum = pageNum * pageSize - pageSize;
        List<Period> courseList = periodMapper.pageList(pageNum, pageSize, name, courseId);
        Long count = periodMapper.findCount(name, courseId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", courseList);
        map.put("total", count);
        return Result.success(map);
    }

    /**
     * 学员端查询课程具体数据
     */
    @GetMapping({"stuFindPeriod"})
    public Result stuFindPeriod(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam Integer courseId,
                                @RequestParam Integer userId) {
        Integer pageStartNumber = pageNum * pageSize - pageSize;
        HashMap<String, Object> map = new HashMap();
        map.put("records", periodMapper.findPeriodByCourseId(pageStartNumber, pageSize, courseId, userId));
        map.put("total", periodMapper.findPeriodCount(courseId, userId));
        return Result.success(map);
    }
}

